package com.sf.aspectLog.autoconfiguration;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author :Mr 轩少
 * @Date:2020/7/29
 * @Description:
 */
public class ReqeustData {
    private String ip;
    private String url;
    private String httpMethod;
  //  private String classMethod;
    private Map<String,String[]> requestParams;
    private   Object requestBody;


    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        for (Map.Entry<String, String[]> entry : requestParams.entrySet()) {
          sb.append(entry.getKey()).append("=").append(Arrays.toString(entry.getValue()))
            .append(",")
            ;
        }
        sb.append("}");
        return "ReqeustData{" +
                "ip='" + ip + '\'' +
                ", url='" + url + '\'' +
                ", httpMethod='" + httpMethod + '\'' +
                ", requestParams=" +sb.toString() +
                ", requestBody=" + requestBody +
                '}';
    }

    public  static  Builder builder(){
        return new Builder();
    }
    public  static  class Builder{
        private  ReqeustData reqeustData;

        public Builder() {
            reqeustData = new ReqeustData();
        }

        public  Builder setUrl(String url) {
            reqeustData.url = url;
            return  this;
        }
        public  Builder setHttpMethod(String httpMethod) {
            reqeustData.httpMethod = httpMethod;
            return  this;
        }
        public  Builder setIP(String ip) {
            reqeustData.ip = ip;
            return  this;
        }

      /*  public Builder setClassMethod(String classMethod) {
            reqeustData.classMethod = classMethod;
            return  this;
        }*/
        public Builder setRequestParams(Map<String,String[]> params) {
            reqeustData.requestParams =  params;
            return  this;
        }

        /**
         *  请求体的获取只适合于使用了@RequestBody注解的情况
         * @param point
         * @return
         */
        public Builder setRequestBody(ProceedingJoinPoint  point) {
            reqeustData.requestBody =  getRequestBodyByProceedingJoinPoint(point);
            return  this;
        }
        public  ReqeustData  build(){
            return reqeustData;
        }
        private Map<String, Object> buildRequestBody(String[] paramNames, Object[] paramValues) {
            Map<String, Object> requestParams = new HashMap();
            for (int i = 0; i < paramNames.length; i++) {

                Object value = paramValues[i];
                if (value instanceof HttpServletRequest || value instanceof HttpServletResponse) {
                    continue;
                }

                //如果是文件对象
                if (value instanceof MultipartFile) {
                    MultipartFile file = (MultipartFile) value;
                    value = file.getOriginalFilename();  //获取文件名
                }

                requestParams.put(paramNames[i], value);
            }

            return requestParams;
        }
        private Map<String, Object> getRequestBodyByProceedingJoinPoint(ProceedingJoinPoint proceedingJoinPoint) {
            //参数名
            String[] paramNames = ((MethodSignature) proceedingJoinPoint.getSignature()).getParameterNames();
            //参数值
            Object[] paramValues = proceedingJoinPoint.getArgs();

            return buildRequestBody(paramNames, paramValues);
        }

    }




}
